﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SpaceFight
{
    class ParticleStarSheet
    {  
        VertexPositionTexture[] verts;
        Color[] vertexColorArray;
        VertexBuffer particleVertexBuffer;

        Vector3 maxPosition;
        int maxParticles;
        static Random random = new Random();

        GraphicsDevice graphicsDevice;
        int maxSize = 2;
        Effect particleEffect;

        Texture2D particleColorsTexture;

        public ParticleStarSheet(GraphicsDevice graphicsDevice, Vector3 maxPosition, int maxParticles,
            Effect particleEffect, Texture2D particleColorsTexture)
        {
            this.graphicsDevice = graphicsDevice;
            this.maxPosition = maxPosition;
            this.maxParticles = maxParticles;
            this.particleEffect = particleEffect;
            this.particleColorsTexture = particleColorsTexture;

            InitializeParticleVertices();
        }

        private void InitializeParticleVertices()
        {
            verts = new VertexPositionTexture[maxParticles * 8];
            vertexColorArray = new Color[maxParticles];

            Color[] colors = new Color[particleColorsTexture.Width * particleColorsTexture.Height];
            particleColorsTexture.GetData(colors);

            for (int i = 0; i < maxParticles; ++i)
            {
                float size = (float)random.NextDouble() * maxSize;

               Vector3 position = new Vector3(random.Next(-(int)maxPosition.X, (int)maxPosition.X),
                    random.Next(-(int)maxPosition.Y, (int)maxPosition.Y), maxPosition.Z);
                //Vector3 position = new Vector3(random.Next(), random.Next(), maxPosition.Z);
                verts[i * 4] = new VertexPositionTexture(position, new Vector2(0, 0));
                verts[i * 4 + 1] = new VertexPositionTexture(new Vector3(position.X, position.Y + size, position.Z), new Vector2(0, 1));
                verts[i * 4 + 2] = new VertexPositionTexture(new Vector3(position.X + size, position.Y, position.Z), new Vector2(1, 0));
                verts[i * 4 + 3] = new VertexPositionTexture(new Vector3(position.X + size, position.Y + size, position.Z), new Vector2(1, 1));

                vertexColorArray[i] = colors[(random.Next(0, particleColorsTexture.Height) * particleColorsTexture.Width) + random.Next(0, particleColorsTexture.Width)];

            }

            for (int i = 0; i < maxParticles; ++i)
            {
                float size = (float)random.NextDouble() * maxSize;

                Vector3 position = new Vector3(random.Next(-(int)maxPosition.X, (int)maxPosition.X),
                     random.Next(-(int)maxPosition.Y, (int)maxPosition.Y), maxPosition.Z);
                //Vector3 position = new Vector3(random.Next(), random.Next(), maxPosition.Z);
                verts[i * 8] = new VertexPositionTexture(position, new Vector2(0, 0));
                verts[i * 8 + 1] = new VertexPositionTexture(new Vector3(position.X, position.Y + size, position.Z), new Vector2(0, 1));
                verts[i * 8 + 2] = new VertexPositionTexture(new Vector3(position.X + size, position.Y, position.Z), new Vector2(1, 0));
                verts[i * 8 + 3] = new VertexPositionTexture(new Vector3(position.X + size, position.Y + size, position.Z), new Vector2(1, 1));

                vertexColorArray[i] = colors[(random.Next(0, particleColorsTexture.Height) * particleColorsTexture.Width) + random.Next(0, particleColorsTexture.Width)];

            }

            
            particleVertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionTexture), verts.Length, BufferUsage.None);
        }


        public void Draw(Camera camera)
        {
            graphicsDevice.SetVertexBuffer(particleVertexBuffer);

            for (int i = 0; i < maxParticles; ++i)
            {
                particleEffect.Parameters["WorldViewProjection"].SetValue(camera.view * camera.projection);
                particleEffect.Parameters["particleColor"].SetValue(vertexColorArray[i].ToVector4());

                foreach (EffectPass pass in particleEffect.CurrentTechnique.Passes)
                {
                    pass.Apply();

                    graphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, verts, i * 4, 2);
                }
            }
        }
    }
}

 